let list_rank = require('../api/kugou/playlist/rank');
let search = require('../api/kugou/search');
let play = require('../api/kugou/play');
let lrc = require('../api/kugou/lrc');
let detail = require('../api/kugou/playlist/detail');
export async function getKgPlayListDetail(id,f){

  await detail(id).then((r) => {
    // console.log(r)
    let result = dealDetailResult(r)
    // console.log(result)
    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function dealDetailResult(data){
  let {info} = data;
  let {list:{nickname:user,imgurl:img,playcount:playCount,collectcount:subscribedCount,specialname:name,specialid:id,intro:description}} = info;
  let tags = info.list.tags.map((i) => {
    return i.tagname
  })
  while (img.indexOf('{size}')!==-1){
    img = img.replace('{size}','150');
  }
  let tracks = data.list.list.info;
  let r = tracks.map((i) => {
    let name = i.filename.split(" - ");
    let ts = i.duration;
    let min = Math.floor(ts/60);
    let sec = Math.floor(ts - min*60);
    if(min < 10){
      min = '0' + min;
    }
    if(sec < 10){
      sec = '0' + sec;
    }
    let time = `${min} : ${sec}`;
    return{
      'times' : time,
      'name' : name[1],
      'singer' : name[0],
      'album' : i.remark,
      'id' : i.hash,
      from:'kugou'
    }
  })


  return {
    user:user,
    tags:tags,
    name:name,
    id:id,
    description:description,
    img:img,
    playCount:playCount,
    subscribedCount:subscribedCount,
    list:r,
    from:'kugou'
  }
}
export async function top_play_list_KG(f){

  await list_rank().then((r) => {
    let result = []
    for (let playlist of r.plist.list.info) {
      result.push(dealkgResult(playlist))
    }
    //console.log("@@@kg",result.length)

    f(result)
  })
}

function dealkgResult(data){
  let {specialname:name,specialid:id,imgurl:imgUrl,username : auth} = data;



  return {
    "name":name,
    "id":id,
    "img":imgUrl.replace('{size}','150'),
    "auth":auth,
    "from":'kugou'
  }
}

export async function getKgSearch(keywords,page,pagesize,f){
  //console.log(keywords);
  await search(keywords,page,pagesize).then((r) => {

    //console.log(r)
    //console.log('rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr')
    let result = dealSearchResult(r.data)
    let count = r.data?.total || 0;
    f({v:result,count:count})
  })
}
function dealSearchResult(data) {

  let tracks = data.info;
  let r = tracks.map((i) => {
    let ts = i.duration;
    let min = Math.floor(ts/60);
    let sec = Math.floor(ts - min*60);
    if(min < 10){
      min = '0' + min;
    }
    if(sec < 10){
      sec = '0' + sec;
    }
    let time = `${min} : ${sec}`;
    return{
      'times' : time,
      'name' : i.songname,
      'singer' : i.singername,
      'album' : i.album_name,
      'id' : i.hash,
      'from' : 'kugou'
    }
  })
  return r
}

export async function getKgPlayMusic(id,f){
  await play(id).then((r) => {

    //console.log(r)
    if(!r.url){
      f(-1)
    }else {
      let result = dealPlayMusicResult(r)
      ////console.log(result)
      f(result)
    }
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
/**
 * {
 *                     id: "1",
 *                     title: "打上花火",
 *                     info: "DAOKO,米津玄師",
 *                     // resource: require("./resource/DAOKO,米津玄師 - 打上花火.mp3"),
 *                     resource: "",
 *                     time: "04:50",
 *                     img:
 *                         "http://singerimg.kugou.com/uploadpic/softhead/400/20160913/20160913140233132.jpg"
 *                 },
 */
function dealPlayMusicResult(data){
  let {fileName} = data;
  let s = fileName;
  while(s.indexOf(' ') !== -1){
    s = s.replace(' ','');
  }
  let nAndT = s.split('-');

  let {hash: id, album_img,url,timeLength,albumid} = data

  let info = nAndT[0]
  let title = nAndT[1]
  let img = album_img
  while(img.indexOf('{size}') !== -1){
    img = img.replace('{size}','150');
  }
  let resource = url;
  let ts = timeLength;
  let min = Math.floor(ts/60);
  let sec = Math.floor(ts - min*60);
  if(min < 10){
    min = '0' + min;
  }
  if(sec < 10){
    sec = '0' + sec;
  }
  let time = `${min} : ${sec}`;

  return {
    id:id,
    title:title,
    info:info,
    resource:resource,
    time:time,
    from:'kugou',
    albumid:albumid,
    img:img
  }
}
export async function getKglyric(id,albumid,f){
  await lrc(id,albumid).then((r) => {
    //console.log(r)
    let result = deallyricResult(r)
    while(result.indexOf('\r') !== -1){
      result = result.replace('\r','');
    }

    f(result)
  }).catch(() => {

    return new Promise((resolve, reject) => {
      reject('err');
    })

  })
}
function deallyricResult(data){

  let list = data.data.lyrics || ""


  return list;

}
